package com.thf.service.impl;

import com.thf.dao.ProductDao;
import com.thf.model.PageModel;
import com.thf.model.ResultData;
import com.thf.model.product.ProductModel;
import com.thf.service.ProductService;
import com.thf.util.ResultTool;
import com.thf.util.Tool;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ProductServiceImpl implements ProductService {
    @Resource
    private ProductDao productDao;

//    public static void main(String[] args) {
//        BigDecimal price= new BigDecimal("6000");
//        System.out.println(price);
//    }
    @Override
    public ResultData productFindAll(PageModel pageModel, Map map) {
        ProductModel productModel = new ProductModel();
        productModel.setProductName(Tool.jsonToString(map.get("name")));
        productModel.setCreateTime(Tool.jsonToString(map.get("start")));
        productModel.setUpdateTime(Tool.jsonToString(map.get("end")));
        BigDecimal price= new BigDecimal(Tool.StringToNotNull((String)map.get("price")));
        productModel.setPrice(price);
        productModel.setProductType(Tool.jsonToString(map.get("type")));
        productModel.setEnable(Tool.jsonToString(map.get("enable")));
        List<ProductModel> list = null;
            list = productDao.findAll(pageModel, productModel);
        if (list == null) {
            return new ResultData(ResultTool.CODE_ERROR, ResultTool.CODE_ERROR_MSG);
        }
        return new ResultData(ResultTool.CODE_SUCCES, ResultTool.CODE_SUCCES_MSG, list);
    }

    @Override
    public Integer getCount(Map map) {
        ProductModel productModel = new ProductModel();
        productModel.setProductName(Tool.jsonToString(map.get("name")));
        productModel.setCreateTime(Tool.jsonToString(map.get("start")));
        productModel.setUpdateTime(Tool.jsonToString(map.get("end")));
        BigDecimal price= new BigDecimal(Tool.StringToNotNull((String)map.get("price")));
        productModel.setPrice(price);
        productModel.setProductType(Tool.jsonToString(map.get("type")));
        productModel.setEnable(Tool.jsonToString(map.get("enable")));
        Integer count = null ;
        if(productModel != null){
            count = productDao.getCount(productModel);
        }
        return count;
    }

    @Override
    public ResultData add(Map map) {
        ProductModel productModel = new ProductModel();
        productModel.setProductName(Tool.jsonToString(map.get("name")));
        BigDecimal price= new BigDecimal(Tool.StringToNotNull((String)map.get("price")));
        BigDecimal normalPrice= new BigDecimal(Tool.StringToNotNull((String)map.get("normalPrice")));
        productModel.setPrice(price);
        productModel.setNormalPrice(normalPrice);
        productModel.setProductType(Tool.jsonToString(map.get("type")));
        productModel.setEnable(Tool.jsonToString(map.get("enable")));
        productModel.setImg(Tool.jsonToString(map.get("url")));
        productModel.setContent(Tool.jsonToString(map.get("describe")));
        productModel.setCreateTime(Tool.dateToString(new Date()));
        Integer count = null ;
        if(productModel!=null){
            count = productDao.add(productModel);
        }
        if(count ==0){
            return new ResultData(ResultTool.CODE_ERROR, ResultTool.CODE_ADD_ERROR_MSG);
        }
        return new ResultData(ResultTool.CODE_SUCCES, ResultTool.CODE_ADD_SUCCES_MSG,count);
    }

    @Override
    public ResultData getProductById(Integer pid) {
        ProductModel productModel = null;
        if(pid != null){
            productModel = productDao.getProductById(pid);
        }
        if(productModel == null){
            return new ResultData(ResultTool.CODE_ERROR, ResultTool.CODE_PRODUCT_ID_ERROR_MSG);
        }
        return new ResultData(ResultTool.CODE_SUCCES, ResultTool.CODE_PRODUCT_ID_SUCCES_MSG,productModel);
    }

    @Override
    public ResultData update(Map map) {
        ProductModel productModel = new ProductModel();
        productModel.setProductName(Tool.jsonToString(map.get("name")));
        BigDecimal price= new BigDecimal(Tool.StringToNotNull((String)map.get("price")));
        BigDecimal normalPrice= new BigDecimal(Tool.StringToNotNull((String)map.get("normalPrice")));
        productModel.setPrice(price);
        productModel.setNormalPrice(normalPrice);
        productModel.setProductType(Tool.jsonToString(map.get("type")));
        productModel.setEnable(Tool.jsonToString(map.get("enable")));
        productModel.setImg(Tool.jsonToString(map.get("url")));
        productModel.setContent(Tool.jsonToString(map.get("describe")));
        productModel.setId((Integer) map.get("id"));
        productModel.setUpdateTime(Tool.dateToString(new Date()));
        Integer count = null ;
        if(productModel!=null){
            count = productDao.update(productModel);
        }
        if(count ==0){
            return new ResultData(ResultTool.CODE_ERROR, ResultTool.CODE_ADD_ERROR_MSG);
        }
        return new ResultData(ResultTool.CODE_SUCCES, ResultTool.CODE_ADD_SUCCES_MSG,count);
    }

    @Override
    public ResultData delete(Integer pid) {
        Integer count = null ;
        if(pid != null){
            count = productDao.delete(pid);
        }
        if(count ==0){
            return new ResultData(ResultTool.CODE_ERROR, ResultTool.CODE_DEL_ERROR_MSG);
        }
        return new ResultData(ResultTool.CODE_SUCCES, ResultTool.CODE_DEL_SUCCES_MSG,count);
    }
}
